// pages/sort/index.js
const app = getApp()
Page({
  data: {
    serverHost: app.config.serverHost,
    value:'',
    screenWidth: 0,
    imgwidth: 0,
    imgheight: 0,
    tabList: ["全部","时令水果","新鲜蔬菜","肉禽蛋类","海鲜水产","酒水饮料","零食熟食","粮油速食","纸品清洁"],
    goodsList: '',
    filterData:[],
    active:0,
    serverHost: app.config.serverHost
  },
  // 点击跳转到商品详情页
  toDetail: (e) => {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../goodsDetail/goods?id=' + id
    })
  },
  toDetails: (e) => {
    wx.showToast({
      title: '商品已售空',
      duration: 1000,
      image:'../../static/icons/err.png',
      mask: true    //是否有透明蒙层，默认为false 
      //如果有透明蒙层，弹窗的期间不能点击文档内容 
    })
  },
  // 添加购物车
  add:function(e){
    if (!wx.getStorageSync('token')) {
      wx.showToast({
        title: '请先登录...',
        image: '../../static/icons/err.png',
        duration: 2000,
        success: function (res) {
          wx.navigateTo({
            url: '../my/loging/index',
          })
        }
      })
      return
    }
    let id = e.currentTarget.dataset.id // 获取传入的id
    let index = e.currentTarget.dataset.index  // 获取数据的索引
    let temp = 'goodsList[' + index +'].value'  // 获取goodsList[index].value
    this.setData({
      [temp]:this.data.goodsList[index].value + 1
    })
    var shopping_cart = wx.getStorageSync('shopping_cart')
    var a = shopping_cart.findIndex(item => {
      return item.goods == id
    })
    a >= 0 ? shopping_cart.splice(a, 1, { goods: id, value: shopping_cart[a].value + 1 }) : shopping_cart.push({ goods: id, value: 1 })
    wx.setStorageSync('shopping_cart', shopping_cart)
  },
  // 切换选中的标签栏状态
  changeActive:function(a){
    this.sift(a.detail.title)
  },
  // 筛选商品
  sift(a){
    let item = this.data.filterData
    let i = item.filter(e => {
      return a == '全部' ? item : e.class == a
    })
    this.setData({
      goodsList:i
    })
  },
  // 搜索函数
  onSearch:function(e){
    let _this = this
    let temp = this.data.filterData
    var list = temp.filter(data => {
      return Object.keys(data).some(key => {
        return String(data[key]).match(e.detail)
      })
    })
    // if(a.detail == ''){
    //   _this.onClear()
    // }else{
    //   let i = temp.filter(e => {
    //     return e.name.indexOf(a.detail) >= 0
    //   })
      this.setData({
        goodsList:list
      })
    
  },
  // 清除搜索框函数
  onClear(){
    this.setData({
      goodsList:this.data.filterData,
      active: 0
    })
  },
  onLoad(){
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      filterData: app.globalData.goodslist,
      goodsList: app.globalData.goodslist
    })
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          screenWidth: res.windowWidth,
        });
      }
    });
    let shopping_cart = wx.getStorageSync('shopping_cart')
    app.globalData.goodslist.filter(e => {
       Object.values(shopping_cart).some(key => {
         e._id == key.goods ? e.value = key.value : ''
      })
      return true
    })
  },
  onShow(){
    this.sift(app.globalData.text)
    app.globalData.text == '全部' ? this.setData({active:app.globalData.index}) : this.setData({active:app.globalData.index})
    wx.hideLoading();
  },
  onHide() {
    app.globalData.index = 0
    app.globalData.text = '全部'
    if (!wx.getStorageSync('token') || wx.getStorageSync('shopping_cart').length == 0) {
      return
    }
    const model = {
      token: wx.getStorageSync('token'),
      shopping_cart: wx.getStorageSync('shopping_cart')
    }
    app.api.updateData('web/goodscart', model, (res) => {})
  },
  imageLoad(){
    var screenW = this.data.screenWidth;
    var viewWidth = 320 * screenW / 750;
    var viewHeight = viewWidth;
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  }
})

